<?php namespace App\Controllers\Manager;

//http://www.codeforest.net/laravel4-simple-website-with-backend-1
 
use App\Models\Job;
use App\Models\JobFile;
use Input, Redirect, Sentry, Str, Response, Config;
use Notification;
 
class JobsController extends \BaseController {
 
    public function index()
    {
        return \View::make('manager.jobs.index')
            ->with('jobs', Job::all());
    }
 
    public function show($id)
    {
        /*return \View::make('manager.pages.show')
            ->with('page', Page::find($id));*/
    }
 
    public function create()
    {
	    $jobtypes = array();
	    foreach(\DB::select('select * from job_types') as $jobtype)
	    {
		    $jobtypes[$jobtype->id] = $jobtype->jobtype;
	    }

        return \View::make('manager.jobs.create')
	        ->with("jobtypes", $jobtypes);
    }
 
    public function store()
    {
        $job = new Job;
	    $job->title = Input::get("title");
	    $job->slug = Str::slug(Input::get('slug'));
	    $job->jobtypeid = Input::get("jobtypeid");
	    $job->position = Input::get("position");
	    $job->location = Input::get("location");
	    $job->salary = Input::get("salary");
	    $job->description = Input::get("description");

	    $job->save();
        
        Notification::success('The job was saved.');
     
        return Redirect::route('manager.jobs.index');
    }
 
    public function edit($id)
    {
        $job = Job::find($id);
	    $jobtypes = array();
	    foreach(\DB::select('select * from job_types') as $jobtype)
	    {
		    $jobtypes[$jobtype->id] = $jobtype->jobtype;
	    }
		return \View::make('manager.jobs.edit')
            ->with('job', $job)
			->with("jobtypes", $jobtypes);
    }
 
    public function update($id)
    {
	    $job = Job::find($id);
	    $job->title = Input::get("title");
	    $job->slug = Str::slug(Input::get('slug'));
	    $job->jobtypeid = Input::get("jobtypeid");
	    $job->position = Input::get("position");
	    $job->location = Input::get("location");
	    $job->salary = Input::get("salary");
	    $job->description = Input::get("description");
        
		$job->save();
		
		Notification::success('The job was saved.');
     
        return Redirect::route('manager.jobs.edit', $job->id);
    }
 
    public function destroy($id)
    {
        $job = Job::find($id);
	    $job->delete();

        Notification::info('The job was deleted.');

        return Redirect::route('manager.jobs.index');
    }

    public function getSlug()
    {
        return Response::json(array(
            "val" => Str::slug(Input::get("str"))
        ));
    }
	
	public function uploadFile($id)
	{
		$job = Job::find($id);
		
		if (Input::hasFile('thefile'))
		{
			$destinationPath = Config::get('app.jobfilepath').$id;
			$path_parts = pathinfo(Input::file('thefile')->getClientOriginalName());
			
			$newfilename = Str::slug($path_parts["filename"]).".".$path_parts["extension"];
			
			if(!file_exists($destinationPath."/".$newfilename))
			{
				Input::file('thefile')->move($destinationPath, $newfilename);
			
				$jf = new JobFile;
				$jf->filename = $newfilename;
				$jf->save();
				
				$job->files()->attach($jf->id);
			}
			
			return Response::json(array(
				"error" => 0 //Str::slug(Input::get("str"))
			));
		}
		else
		{
			return Response::json(array(
				"error" => "No file specified" //Str::slug(Input::get("str"))
			));
		}
	}
	
	public function getFiles($jobid)
	{
		return \View::make('manager.jobs.files')
            ->with('job', Job::find($jobid));
	}
	
	public function deleteFile($jobid, $fileid)
	{
		$job = Job::find($jobid);
		$file = JobFile::find($fileid);
		
		$job->files()->detach($file->id);
		if(file_exists(Config::get('app.jobfilepath').$jobid."/".$file->filename))
		{
			unlink(Config::get('app.jobfilepath').$jobid."/".$file->filename);
		}
		$file->delete();
	}
}